from os.path import dirname, realpath, basename, join
from mg_app_framework import AppConfigBasic, AppType, RabbitMQConfigBasic, IdiServerConfigBasic, \
    get_mongodb_cluster_info

from common_bind_service.utils.msg import rabbitmq_message_key_to_handler, msg_dispatch


class ConfigStore(AppConfigBasic):
    # ==================== DON'T MODIFY THE CODE BETWEEN COMMENT LINE ====================

    work_dir = dirname(dirname(dirname(realpath(__file__))))
    app = basename(dirname(realpath(__file__)))
    log_path = join(work_dir, 'log', app + '.log')
    uuid_path = join(work_dir, '.appid')

    def get_module_dir(self):
        return dirname(realpath(__file__))

    def get_log_path(self):
        return self.log_path

    def get_uuid_path(self):
        return self.uuid_path

    def get_data(self):
        return self.data

    # ==================== DON'T MODIFY THE CODE BETWEEN COMMENT LINE ====================

    data = {
        'app_group': '应用组件',
        'app_name': '容器与转换服务',
        'app_type': AppType.user,
        'app_version': '',
        'data': {
            'key_list': ['data_mode'],
            'config_data':
                {
                    'data_mode': {
                        'group_name': '参数设置',
                        'key_list': ['param_set'],
                        'group_member': {
                            'param_set': {
                                'key_name': 'FMS接口地址',
                                'key_type': 'string',
                                'key_help': 'True: 设置FMS接口地址',
                                'value': ''
                            },

                        }
                    },

                }
        }
    }

    def get_admin_host(self):
        return 'localhost'

    def get_fms_url(self):
        return self.data['data']['config_data']['data_mode']['group_member']['param_set']['value']

    def get_app_port(self):
        return 38822


class RabbitMQConfig(RabbitMQConfigBasic):
    def publish_config_data(self):
        return False

    def get_rabbitmq_subscribe_list(self):
        return list(rabbitmq_message_key_to_handler.keys())

    def message_process_func(self):
        return msg_dispatch

    def ack_before_message_process_func(self):
        return False

    def set_qos_num(self):
        return 5


class IdiServerConfigBasic1(IdiServerConfigBasic):
    def get_mongodb_host(self):
        return get_mongodb_cluster_info().get('host')

    def get_mongodb_port(self):
        return get_mongodb_cluster_info().get('port')

    def retry_interval(self):
        return 5
